package org.ocean.online.web.workspace;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import org.apache.commons.io.FileUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.ocean.online.utils.CommonUtil;
import org.ocean.online.utils.UserUtil;
import org.ocean.online.wicket.NeedLoginPage;
import org.ocean.online.wicket.SimpleAjaxSubmitLink;

public class EditFile extends NeedLoginPage{

	private static final long serialVersionUID = -6994438839560663766L;

	TextField<String> path= new TextField<String>("filePath",new Model<String>(""));
	HiddenField<String> content= new HiddenField<String>("content",new Model<String>(""));
	HiddenField<String> mode= new HiddenField<String>("mode",new Model<String>(""));
	public EditFile(){
		Form<String> form = new Form<String>("ajaxForm",new Model<String>());
		add(form);
		form.add(path);
		form.add(content);
		form.add(mode);
		
		path.setDefaultModelObject(this.getStringParam("path"));
		this.setFileMode();
		SimpleAjaxSubmitLink saveFileLink = new SimpleAjaxSubmitLink("saveFileLink") {

			@Override
			protected void onSubmit(AjaxRequestTarget target) throws Exception {
				UserUtil.saveFile(getUsername(), path.getDefaultModelObjectAsString(),
						new ByteArrayInputStream(content.getDefaultModelObjectAsString().getBytes()));
			}

		};
		form.add(saveFileLink);
		
		File file = new File(this.getWorkspace()+path.getDefaultModelObjectAsString());
		long size =file.length();//B
		if(size>1000*1000){
			//>1MB
			throw new RuntimeException("file is too big to open");
		}
		try {
			content.setDefaultModelObject(FileUtils.readFileToString(file));
		} catch (IOException e) {
			e.printStackTrace();
			content.setDefaultModelObject("open file error: "+e.getMessage());
		}
	}
	
	private void setFileMode(){
		String ext = CommonUtil.getExtOfFile(this.path.getDefaultModelObjectAsString());
		if("js".equalsIgnoreCase(ext)){
			mode.setDefaultModelObject("javascript");
		}else if("css".equalsIgnoreCase(ext)){
			mode.setDefaultModelObject("css");
		}else if("html".equalsIgnoreCase(ext)){
			mode.setDefaultModelObject("html");
		}else if("".equalsIgnoreCase(ext)){
			mode.setDefaultModelObject("html");
		}else if("xml".equalsIgnoreCase(ext)){
			mode.setDefaultModelObject("xml");
		}else if("jsp".equalsIgnoreCase(ext)){
			mode.setDefaultModelObject("jsp");
		}else if("jpg,jpeg,bmp,raw,png,gif,swf".contains(ext.toLowerCase())){
			ViewOnlyPage page = new ViewOnlyPage();
			page.getPageParameters().add("path", this.getWorkspace()+path.getDefaultModelObjectAsString());
			this.setResponsePage(page);
		}else if("action".equalsIgnoreCase(ext)){
			mode.setDefaultModelObject("javascript");
		}else if("model".equalsIgnoreCase(ext)){
			File file = new File(this.getWorkspace()+path.getDefaultModelObjectAsString());
			PageParameters param = new PageParameters();
			param.add("projectName", file.getName());
			this.setResponsePage(DesignModel.class,param);
		}else{
			mode.setDefaultModelObject("html");
		}
	}
}
